layui.use('laydate', function(){
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
        elem: '#start' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
        elem: '#end' //指定元素
    });
});

/*用户-停用*/
function member_stop(obj,id){
    layer.confirm('确认要停用吗？',function(index){

        if($(obj).attr('title')=='启用'){

            //发异步把用户状态进行更改
            $(obj).attr('title','停用')
            $(obj).find('i').html('&#xe62f;');

            $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
            layer.msg('已停用!',{icon: 5,time:1000});

        }else{
            $(obj).attr('title','启用')
            $(obj).find('i').html('&#xe601;');

            $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
            layer.msg('已启用!',{icon: 5,time:1000});
        }

    });
}

/*用户-删除*/
function member_del(obj,id,sid,lang){

    layer.confirm('确认要删除吗？',function(index){
        //发异步删除数据
        $.ajax({
            dataType : 'json',
            type : 'POST',
            url : 'delete',
            async: false,
            data : {id:id,sid:sid,lang:lang},
            success : function(msg){
                layer.alert(msg.msg, {icon: 6},function () {
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!',{icon:1,time:1000});
                });
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status + "," + textStatus);
            }
        });
    });
}

function delAll (argument) {

    var data = tableCheck.getData();

    layer.confirm('确认要删除吗？'+data,function(index){
        //捉到所有被选中的，发异步进行删除
        layer.msg('删除成功', {icon: 1});
        $(".layui-form-checked").not('.header').parents('tr').remove();
    });
}

//分页
var total = $("#total").val()
layui.use(['laypage', 'layer'], function() {
    var laypage = layui.laypage
        , layer = layui.layer;
    console.log(total);
    //自定义首页、尾页、上一页、下一页文本
    laypage.render({
        elem: 'demo3'
        , count: total
        , limit:10
        , first: '首页'
        , last: '尾页'
        , prev: '<em>←</em>'
        , next: '<em>→</em>'
        ,jump: function(obj, first){
            //obj包含了当前分页的所有参数，比如：
            console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            console.log(obj.limit); //得到每页显示的条数
            var page = obj.curr;
            var limit = obj.limit;
            var sid = $("#sid").val();//获取当前站点id
            //首次不执行
            if(!first){
                //do something
                //发异步请求下一页数据
                $.ajax({
                    dataType : 'json',
                    type : 'POST',
                    url : 'list?sid='+sid,
                    async: false,
                    data : {page:page,limit:limit},
                    success : function(data){
                        console.log(data);
                        integratingData(data);

                        $(".xxkk").mouseover(function() {
                            var val=$(this).attr("id");
                            $("."+val).addClass("layui-show");
                        });

                        $(".xxkk").mouseout(function() {
                            var val=$(this).attr("id");
                            $("."+val).removeClass("layui-show");
                        });

                    },
                    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status + "," + textStatus);
                    }
                });
            }
        }
    });
})

function  integratingData(data){
    var html = '';
    for(var i=0;i<data.length;i++){

        html+=
            '<tr>'+
            '<td><div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id="2"><i class="layui-icon">&#xe605;</i></div></td>' +
            '<td>'+data[i].number+'</td>';
        if(data[0].lm == 0) {
            html+=
                '<td>' + data[i].zhtitle + '</td>' +
                '<td>' + data[i].entitle + '</td>' +
                '<td>' + data[i].zhdesc + '</td>' +
                '<td>' + data[i].endesc + '</td>' ;
        }
        if(data[0].lm == 1) {
            html+=
                '<td>' + data[i].zhtitle + '</td>' +
                '<td>' + data[i].zhdesc + '</td>' ;
        }
        if(data[0].lm == 2) {
            html+=
                '<td>' + data[i].entitle + '</td>' +
                '<td>' + data[i].endesc + '</td>' ;
        }
        html+=
            '<td>' + data[i].created + '</td>' +
            '<td class="td-status">';
        if(data[i].status == 1){
            html+=
                `<span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>`+
                `</td>`+
                `<td class="td-manage">`+
                `<a onclick="member_stop(this,10001)" href="javascript:;"  title="启用">`+
                `<i class="layui-icon">&#xe601;</i>`+
                `</a>`+
                `<a title="编辑"  onclick=(` +
                `x_admin_show('编辑',`+
                `'details?id=`+data[i].uid+`&sid=`+data[0].sid+
                `')) href="javascript:;">`+
                `<i class="layui-icon">&#xe642;</i>`+
                `<span class="layui-nav-more"></span></a>`;
            if(data[0].lm == 0) {
                html+=
                    `<a style="width: 15px"><ul   class="layui-nav scul" style=""><li id="z` + data[i].uid + `" class="layui-nav-item xxkk" style="color: #0C0C0C;height: 70px;" ><a style="width: 15px">` +
                    `<span class="layui-nav-more"></span></a>` +
                    `<a href="javascript:;" style="color: #0C0C0C">` +
                    `<i style="font-size: 17px;" class="layui-icon">&#xe640;</i>` +
                    `</a>` +
                    `<dl  class="layui-nav-child layui-anim layui-anim-upbit z` + data[i].uid + `"  style="top:40px;">` +
                    `<dd><a onclick="member_del(this, '` +
                    data[i].uid +
                    `','` + data[0].sid +
                    `','` + data[i].zhlang +
                    `')" href="javascript:;">仅中文</a></dd>` +
                    `<dd><a onclick="member_del(this, '` +
                    data[i].uid +
                    `','` + data[0].sid +
                    `','` + data[i].enlang +
                    `')" href="javascript:;">仅英文</a></dd>` +
                    `<dd><a onclick="member_del(this, '` +
                    data[i].uid +
                    `','` + data[0].sid +
                    `')" href="javascript:;">全删除</a></dd>` +
                    `</dl>` +
                    `</li>` +
                    `</ul>` +
                    `</a>` ;
            }else{
                html+=
                    `<a title="删除" onclick="member_del(this,'` +
                    data[i].uid+
                    `','` + data[0].sid +
                    `')" href="javascript:;">`+
                    '<i class="layui-icon">&#xe640;</i>'+
                    '</a>';
            }
            html+=
                '</td>'+
                '<tr>';
        }else{
            html+=
                `<span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>`+
                `</td>`+
                `<td class="td-manage">`+
                `<a onclick="member_stop(this,10001)" href="javascript:;"  title="启用">`+
                `<i class="layui-icon">&#xe601;</i>`+
                `</a>`+
                `<a title="编辑"  onclick=(` +
                `x_admin_show('编辑',`+
                `'details?id=`+data[i].uid+`&sid=`+data[0].sid+
                `')) href="javascript:;">`+
                `<i class="layui-icon">&#xe642;</i>`+
                `<span class="layui-nav-more"></span></a>`;
            if(data[0].lm == 0) {
                html+=
                    `<a style="width: 15px"><ul   class="layui-nav scul" style=""><li id="z` + data[i].uid + `" class="layui-nav-item xxkk" style="color: #0C0C0C;height: 70px;" ><a style="width: 15px">` +
                    `<span class="layui-nav-more"></span></a>` +
                    `<a href="javascript:;" style="color: #0C0C0C">` +
                    `<i style="font-size: 17px;" class="layui-icon">&#xe640;</i>` +
                    `</a>` +
                    `<dl  class="layui-nav-child layui-anim layui-anim-upbit z` + data[i].uid + `"  style="top:40px;">` +
                    `<dd><a onclick="member_del(this, '` +
                    data[i].uid +
                    `','` + data[0].sid +
                    `','` + data[i].zhlang +
                    `')" href="javascript:;">仅中文</a></dd>` +
                    `<dd><a onclick="member_del(this, '` +
                    data[i].uid +
                    `','` + data[0].sid +
                    `','` + data[i].enlang +
                    `')" href="javascript:;">仅英文</a></dd>` +
                    `<dd><a onclick="member_del(this, '` +
                    data[i].uid +
                    `','` + data[0].sid +
                    `')" href="javascript:;">全删除</a></dd>` +
                    `</dl>` +
                    `</li>` +
                    `</ul>` +
                    `</a>` ;
            }else{
                html+=
                    `<a title="删除" onclick="member_del(this,'` +
                    data[i].uid+
                    `','` + data[0].sid +
                    `')" href="javascript:;">`+
                    '<i class="layui-icon">&#xe640;</i>'+
                    '</a>';
            }
            html+=
                '</td>'+
                '<tr>';
        }
    }
    $('#contentbody').html(html);

}